package com.gjy.poi;

import com.google.common.collect.Sets;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-06-06 09:15:34
 */
public class ExcelDataHandler {


    /**
     * 取出2个Excel文件中的某个字段的差集
     * e1 - e2
     *
     * @param e1 第一个Excel文件全路径
     * @param e2 第二个Excel文件全路径
     * @return 字段差值的集合
     */
    public static Set<String> diff(String e1, String e2, Integer column) throws IOException {
        HashSet<String> set1 = Sets.newHashSet();
        HashSet<String> set2 = Sets.newHashSet();

        Workbook workbook = new XSSFWorkbook(Files.newInputStream(Paths.get(e1)));
        Sheet sheet = workbook.getSheetAt(0);
        for (Row row : sheet) {
            RichTextString value = row.getCell(column).getRichStringCellValue();
            set1.add(value.getString());
        }
        System.out.println("set1 = " + set1);
        workbook.close();

        Workbook workbook2 = new XSSFWorkbook(Files.newInputStream(Paths.get(e2)));
        Sheet sheet2 = workbook2.getSheetAt(0);
        for (Row row : sheet2) {
            String value = row.getCell(column).getStringCellValue();
            set2.add(value);
        }
        System.out.println("set2 = " + set2);
        workbook2.close();

        return Sets.difference(set1, set2);
    }
}
